#include "Dathuc.h"



Dathuc::Dathuc()
{
}

Dathuc::Dathuc(const Dathuc &dt)
{
	donthuc = dt.donthuc;
}

void Dathuc::Deletespace(string &chuoi)
{
	int i = 0;
	while (i < chuoi.size())
	{
		if (chuoi[i] == ' ')
			chuoi.erase(i, 1);
		else
			++i;
	}
}

Dathuc::Dathuc(string chuoi)
{
	Deletespace(chuoi);
	if (!chuoi.empty())
	{
		int i = 0;
		while (i < chuoi.size()) //them dau + vao truoc dau -
		{
			i = chuoi.find('-',i);
			if (i != string::npos)
			{
				chuoi.insert(i, "+");
				i += 2;
			}
			else
				i = chuoi.size();
		}
		while (!chuoi.empty())
		{
			int found = chuoi.find('+');
			if (found != string::npos)
			{
				donthuc.Add(chuoi.substr(0, found));
				chuoi = chuoi.substr(found + 1, chuoi.size() - found - 1);
			}
			else
			{
				donthuc.Add(chuoi.substr(0, found));
				chuoi = "";
			}
		}
	}
}

void Dathuc::Sort()
{
	for (int i = 0; i < donthuc.size() - 1; i++)
		for (int j = i+1; j < donthuc.size(); j++)
		{
			if (donthuc.GetValue(i).Somu() > donthuc.GetValue(j).Somu())
			{
				Donthuc temp = donthuc.GetValue(i);
				donthuc.SetValue(i, donthuc.GetValue(j));
				donthuc.SetValue(j, temp);
			}
		}
}


Dathuc::~Dathuc()
{
}

void Dathuc::Rutgon()
{
	for (int i = 0; i < Sodonthuc() - 1; i++)
	{
		for (int j = i + 1; j < Sodonthuc(); j++)
		{
			if (this->donthuc.GetValue(i).Somu() == this->donthuc.GetValue(j).Somu())
			{
				this->donthuc.SetValue(i,this->donthuc.GetValue(i) + this->donthuc.GetValue(j));
				this->donthuc.DeletePosition(j);
			}
		}
	}
	int n = Sodonthuc();
	int i = 0;
	while (i < n)
	{
		if (donthuc.GetValue(i).Heso() == 0)
			donthuc.DeletePosition(i);
		else
			++i;
	}
}

void Dathuc::Add(Donthuc dt)
{
	if (dt.Heso() == 0)
		return;
	donthuc.Add(dt);
}

Dathuc  Dathuc::operator+(Dathuc dt)
{
	Dathuc temp;
	int i, j;
	//dua da thuc 1 vao temp
	for (i = 0; i < this->Sodonthuc(); i++)
		temp.Add(this->donthuc.GetValue(i));
	//cong da thuc hai vao temp
	for (i = 0; i < dt.Sodonthuc(); i++)
	{
		for (j = 0; j < temp.Sodonthuc(); j++) //tim don thuc trong temp co cung so mu voi dt2
			if (dt.donthuc.GetValue(i).Somu() == temp.donthuc.GetValue(j).Somu())
			{
				temp.donthuc.GetValue(j).Heso(temp.donthuc.GetValue(j).Heso() + dt.donthuc.GetValue(i).Heso());
				break;
			}
		if (j == temp.Sodonthuc()) //khong tim thay don thuc trong temp co cung so mu voi dt2
			temp.Add(dt.donthuc.GetValue(i)); // them mot don thuc vao temp
	}
	//sap xep lai da thuc theo so mu giam dan
	temp.Rutgon();
	temp.Sort();
	return temp;
}

Dathuc Dathuc::operator*(Dathuc dt)
{
	Dathuc temp;

	for (int i = 0; i < this->Sodonthuc(); i++)
		for (int j = 0; j < dt.Sodonthuc(); j++)
		{
			string t = std::to_string(this->donthuc.GetValue(i).Heso() * dt.donthuc.GetValue(j).Heso());
			t += "x^";
			t += std::to_string(this->donthuc.GetValue(i).Somu() + dt.donthuc.GetValue(j).Somu());
			Donthuc s(t);
			temp.Add(s);
		}
	temp.Rutgon();
	temp.Sort();
	return temp;
}

string Dathuc::ToString()
{
	if (this->Sodonthuc() == 0)
		return "";

	string chuoi = "";
	for (int i = 0; i < this->Sodonthuc(); i++)
	{
		chuoi += this->donthuc.GetValue(i).ToString();
		if (i + 1 < this->Sodonthuc())
		{
			if (this->donthuc.GetValue(i+1).Heso() > 0)
				chuoi += " + ";
		}
	}
	return chuoi;
}